﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class Employee : BasePage
    {
        protected int totalRecords = 0;
        protected int totalPages = 1;
        private IDepartmentBLL departmentBLL = IocContainer.Instance.Resolve<IDepartmentBLL>();
        private IEmployeeBLL employeeBLL = IocContainer.Instance.Resolve<IEmployeeBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            if(!IsPostBack)
            {
                ControlAuthority(new List<WebControl> { btnAdd, btnUpdate, btnDelete,btnhidDelete, btnSearch }, 25);
                BuildDepartmentTree();
            }
            Query();
        }

       private void BuildDepartmentTree()
       {
            DataTable dtDepartment = departmentBLL.GetAllDepartment();
            this.ddlDepartment.Items.Add(new ListItem("【全部部门】","-1"));
            BindTreeViewToDropdownList(dtDepartment, 0, this.ddlDepartment,-1,"DepartmentName", "DepartmentId");
        }

        protected  void Query()
        {
            int pageIndex = 0;
            int.TryParse(this.pageNum.Value, out pageIndex);
            int pagePerSize = 0;
            int.TryParse(this.numPerPageSelect.Value, out pagePerSize);
            StringBuilder strWhere = new StringBuilder(" where 1=1 ");
            if(!string.IsNullOrEmpty(this.txtEmpName.Text))
            {
                strWhere.Append(" and EmpName like '%"+this.txtEmpName.Text+"%'");
            }
            if(this.ddlDepartment.SelectedValue!="-1")
            {
                strWhere.Append(" and tbe.DeptID=" + this.ddlDepartment.SelectedValue);
            }
            if(this.ddlStatus.SelectedValue!="-1")
            {
                strWhere.Append(" and Status=" + this.ddlStatus.SelectedValue);
            }

            StringBuilder strOrderBy = new StringBuilder(" Order by ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderField.Value) ?" "+this.orderField.Value : "  DepartmentName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderValue.Value) ?" "+this.orderValue.Value : "  asc ");

            DataTable employee = employeeBLL.GetEmployeesByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);
            this.EmpTableBody.Text = BuildEmployeeTable(employee);
        }

        protected string BuildEmployeeTable(DataTable dtEmployee)
        {
            StringBuilder result = new StringBuilder();
            if(dtEmployee!=null && dtEmployee.Rows.Count>0)
            {
                int rowNum=1;
                foreach (DataRow row in dtEmployee.Rows)
                {
                    result.Append(rowNum%2 == 0 ? "<tr class='trbg' rel='"+row["EmpId"]+"'>" : "<tr rel='"+row["EmpId"]+"'>");
                    result.AppendFormat("<td>{0}</td>", row["EmpCode"]);
                    result.AppendFormat("<td>{0}</td>", row["EmpName"]);
                    result.AppendFormat("<td>{0}</td>", row["DepartmentName"]);
                    result.AppendFormat("<td>{0}</td>", row["JobTitle"]);
                    result.AppendFormat("<td>{0}</td>", row["Email"]);
                    result.AppendFormat("<td>{0}</td>", row["Phone"]);
                    result.AppendFormat("<td>{0}</td>", row["FBankName"]);
                    result.AppendFormat("<td>{0}</td>", row["FBankNumber"]);
                    result.AppendFormat("<td>{0}</td>", row["StatusName"]);
                    result.Append("</tr>");
                    rowNum++;
                }
            }
            return result.ToString();

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.pageNum.Value = "1";
            Query();
        }

        protected void btnhidDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int empId = 0;
                int.TryParse(this.selectedEmpID.Value, out empId);
                employeeBLL.DeleteEmployee(empId);

                Query();
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }
            
        }
    }
}
